﻿using Microsoft.EntityFrameworkCore;
using QianJiaInfo.Models;


namespace QianJiaInfo.Service
{
    public class ResidenceContext : DbContext
    {
        public DbSet<Residence> Residences { get; set; }
        public DbSet<Resident> Residents { get; set; }
        public DbSet<Address> Addresses { get; set; }
        public DbSet<PersonRelationship> PersonRelationships { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=QianJia.db");
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            #region 地址
            modelBuilder.Entity<Address>().Property(a => a.Context).IsRequired().HasMaxLength(10);//必须
            modelBuilder.Entity<Address>().Property(a => a.Effective).HasDefaultValue(true);

            #endregion
            #region 户口
            modelBuilder.Entity<Residence>().Property(r => r.Effective).HasDefaultValue(true);
            #endregion
            #region 居民信息
            modelBuilder.Entity<Resident>().HasOne(r => r.ResidenceBlong).WithMany(rr => rr.Residents);//户口和人的多对1关系
            modelBuilder.Entity<Resident>().Property(r => r.Name).HasMaxLength(10).IsRequired();
            modelBuilder.Entity<Resident>().Ignore(r => r.Gender).Ignore(r => r.BirthDate).Ignore(r => r.Age);
            modelBuilder.Entity<Resident>().Property(r => r.CardId).HasMaxLength(18);
            modelBuilder.Entity<Resident>().Property(r => r.PhoneNumber).HasMaxLength(11);
            modelBuilder.Entity<Resident>().Property(r => r.HoseHold).IsRequired().HasDefaultValue(false);

            #endregion
            #region 关系信息
            modelBuilder.Entity<PersonRelationship>().Property(p => p.Relationship).IsRequired();
            modelBuilder.Entity<PersonRelationship>().Property(p => p.Effective).HasDefaultValue(true);

            #endregion

        }
    }

}
